package com.zhu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhu.domain.Inventory;
import com.zhu.mapper.InventoryMapper;
import com.zhu.model.inventory.InventoryItemVM;
import com.zhu.service.InventoryService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author JoyBoy
 * @since 2022-06-02
 */
@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements InventoryService {

    @Override
    public boolean saveList(List<Inventory> inventoryList) {
        return this.saveBatch(inventoryList);
    }

    @Override
    public boolean saveInventory(Inventory inventory) {
        return this.save(inventory);
    }

    @Override
    public boolean deleteById(Integer inventoryId) {
        return this.removeById(inventoryId);
    }

    @Override
    public Inventory getInventoryByCondition(Integer warehouseId, Integer goodsId) {
        QueryWrapper<Inventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("warehouse_id", warehouseId)
                .eq("goods_id", goodsId);
        return this.getOne(queryWrapper);
    }


    @Override
    public List<InventoryItemVM> getByRelevance(Integer warehouseId) {
        return this.getBaseMapper().getByRelevance(warehouseId);
    }

    @Override
    public Integer getGoodsTotalNum(Integer goodsId) {
        QueryWrapper<Inventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id", goodsId);
        List<Inventory> list = this.list(queryWrapper);
        int totalNum = 0;
        for (Inventory inventory : list) {
            totalNum += inventory.getNumber();
        }
        return totalNum;
    }

    @Override
    public boolean updateInventory(Inventory inventory) {
        return this.updateById(inventory);
    }
}
